package com.shiguiwu.springboot3.redis.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @description: 布隆过滤器
 * @author: stone
 * @date: Created by 2023/12/8 22:32
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.redis.filter
 */
@Component
public class BloomFilter {

    @Autowired
    private  RedisTemplate<String, Object> redisTemplate;

    public void addToFilter(String key, String value) {
        int[] hashes = getHashes(value);
        for (int hash : hashes) {
            redisTemplate.opsForValue().setBit(key, hash, true);
        }
    }

    public boolean mightContain(String key, String value) {
        int[] hashes = getHashes(value);
        for (int hash : hashes) {
            Boolean isSet = redisTemplate.opsForValue().getBit(key, hash);
            if (isSet == null || !isSet) {
                return false;
            }
        }
        return true;
    }

    private int[] getHashes(String value) {
        // 实现你的哈希函数，返回一个哈希值数组
        return new int[0];
    }
}
